package com.ydgk.controller;

import com.ydgk.tool.Tool;
import com.ydgk.vo.AjaxResult;
import com.ydgk.bean.Kinds;
import com.ydgk.service.KindsService;
import com.ydgk.vo.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.List;
import java.util.Map;

@RequestMapping("/kinds")
@Controller
public class KindsController {

    @Autowired
    private KindsService kindsService;
    // 上传文件存储目录
    private static final String UPLOAD_DIRECTORY = "upload/kindsFace";
    private static MultipartFile fileForInsert = null;
    private static MultipartFile fileForUpdate = null;

    @ModelAttribute
    public void getKinds(@RequestParam(value = "kid" , required = false , defaultValue = "0") Integer kid, Map map){
             if(kid != 0){
                 Kinds kinds = kindsService.findById(kid);
                 map.put("kinds",kinds);
             }
    }

    @ResponseBody
    @RequestMapping("/kfaceUpload")
    public AjaxResult kfaceUpload(@RequestParam("file") MultipartFile file) throws IOException {
        this.fileForInsert = file;
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/kfaceUpdate")
    public AjaxResult kfaceUpdate(@RequestParam("file") MultipartFile file) throws IOException {
        this.fileForUpdate = file;
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping("/validationExist")
    public AjaxResult validationExist(Kinds kinds,String newKname) {
        AjaxResult ajaxResult = new AjaxResult();
        List<Kinds> kindsList = kindsService.findAll();
        if(kinds.getKid() != null){
           kindsList.remove(kinds);
        }
        for (Kinds k: kindsList) {
            if(k.getKname().equals(newKname)){
                ajaxResult.setSuccess(true);
                return ajaxResult;
            }
        }
        ajaxResult.setSuccess(false);
        return ajaxResult;

    }

    @ResponseBody
    @RequestMapping(value = "/insertKinds", method = RequestMethod.POST)
    public AjaxResult insertKinds(@RequestParam("flag") Boolean flag, HttpServletRequest request, Kinds kinds) throws IOException {
        if (flag) {
            String newfilename = Tool.upload(request,fileForInsert,UPLOAD_DIRECTORY);
            kinds.setKface(UPLOAD_DIRECTORY + "/" + newfilename);
        }else{
            kinds.setKface("img/logo.png");
        }
        int i = kindsService.insert(kinds);
        AjaxResult ajaxResult = new AjaxResult();
        if (i > 0) {
            ajaxResult.setSuccess(true);
        } else {
            ajaxResult.setSuccess(false);
        }
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping(value = "/findAll", method = RequestMethod.POST)
    public AjaxResult findAll() {
        AjaxResult ajaxResult = new AjaxResult();
        try {
            List<Kinds> kindsList = kindsService.findAll();
            Page<Kinds> page = new Page(kindsList, null, null,null,null);
            ajaxResult.setData(page);
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            e.printStackTrace();
        }

    /*    try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping(value = "/deleteKinds/{kid}", method = RequestMethod.DELETE)
    public AjaxResult deleteKinds(@PathVariable("kid") Integer kid, HttpServletRequest request, Kinds kinds) {
        ServletContext application = request.getServletContext();
        AjaxResult ajaxResult = new AjaxResult();
        try {
            int i = kindsService.deleteById(kid);
            if (i > 0) {
                String kface = kinds.getKface();
                if(kface != null && !("".equals(kface)) && !("img/logo.png".equals(kface))){
                    new File(application.getRealPath("/"+kface)).delete();
                }
                ajaxResult.setSuccess(true);
            } else {
                ajaxResult.setSuccess(false);
            }
        } catch (Exception e) {
            ajaxResult.setSuccess(false);
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @ResponseBody
    @RequestMapping(value = "/updateKinds/{kid}", method = RequestMethod.PUT)
    public AjaxResult updateKinds(@RequestParam("flag") Boolean flag, HttpServletRequest request, Kinds kinds) throws IOException {
        if (flag) {
            ServletContext application = request.getServletContext();
            String newfilename = Tool.upload(request,fileForUpdate,UPLOAD_DIRECTORY);
            String kface = kinds.getKface();
            if(kface != null && !("".equals(kface)) && !("img/logo.png".equals(kface))){
                new File(application.getRealPath("/"+kface)).delete();
            }
            kinds.setKface(UPLOAD_DIRECTORY + "/" + newfilename);
        }
        int i = kindsService.updateById(kinds);
        AjaxResult ajaxResult = new AjaxResult();
        if (i > 0) {
            ajaxResult.setSuccess(true);
        } else {
            ajaxResult.setSuccess(false);
        }
        return ajaxResult;
    }
}
